//
//  VIZPictureView.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/16.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import SDWebImage

private let pictureCellId : String = "pictureCellId"

private let pictureCellMargin : CGFloat = 3
private let StatusCellMargin : CGFloat = 8
//获取固定值
private let maxWidth = ScreenWidth - 2 * StatusCellMargin
private let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3

class VIZPictureView: UICollectionView {

    var imageURLs : [VIZCellPicture]? {
        
        didSet{
            print(imageURLs?.count)
            //自动计算pictureView的大小
            let size = self.setPictureSize()
            self.snp_updateConstraints{ (make) -> Void in
                
                make.size.equalTo(size)
            }
            ///更新测试数据
            testLabel.text = "\(imageURLs?.count ?? 0)"
            
            //刷新页面的数据
            self.reloadData()
        }
        
    }
    
    
    //根据配图的数量计算配图的大小
    //1.单张图片显示图片原比例尺寸
    //2.四张图片  2 * 2
    //3. 其他的张图片就按照 3 * n(根据图片数量来计算)
    private func setPictureSize() -> CGSize {
        
        let imageCount = imageURLs?.count ?? 0
        //没有图片
        if imageCount == 0 {
            return CGSizeZero
        }
        
        let layOut = self.collectionViewLayout as! UICollectionViewFlowLayout
        //单张图片
        if imageCount == 1 {
            //TODO 后期完善
            let imageSize = CGSize(width: 100, height: 150)
            layOut.itemSize = imageSize
            return imageSize
        }
        
        layOut.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //四张图片
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureCellMargin
            return CGSize(width: w, height: w)
        }
        
        //程序走到这里 一定是 其他的多张图片
        /*
        1,2,3   - 1
        4,5,6,  - 2
        7,8,9   - 3
        */
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let h = row * itemWidth + (row - 1) * pictureCellMargin
        return CGSize(width: maxWidth, height: h)
    }
    
    
    private lazy var testLabel: UILabel = UILabel(text: "", textColor: UIColor.redColor(), fontSize: 30)
    
    //重写构造方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layOut : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layOut.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layOut.minimumInteritemSpacing = pictureCellMargin
        layOut.minimumLineSpacing = pictureCellMargin
        
        super.init(frame: frame, collectionViewLayout: layOut)
        //注册cell
        self.registerClass(VIZPictureItem.self, forCellWithReuseIdentifier: pictureCellId)
        //设置数据源代理
        self.dataSource = self
        
        setupUI()
    }

    //布局
    private func setupUI(){
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 数据源方法
extension VIZPictureView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! VIZPictureItem
        cell.backgroundColor = randomColor()
        cell.cellPicture = imageURLs![indexPath.item]
        return cell
    }
    
    
}

//pictureCell类
class VIZPictureItem : UICollectionViewCell {
    
    var cellPicture : VIZCellPicture? {
        didSet{
            
            self.pictureView.sd_setImageWithURL(NSURL(string: cellPicture?.thumbnail_pic ?? ""))
//            print("======================")
//            print(cellPicture?.thumbnail_pic)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载PictureView
    lazy var pictureView : UIImageView = {
        
        let v = UIImageView()
        v.contentMode = .ScaleAspectFill
        v.clipsToBounds = true
        
        return v
    }()
    
    private func setUI() {
        contentView.addSubview(pictureView)
        //设置约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(self.contentView.snp_edges)
        }
        //pictureView.backgroundColor = randomColor()
    }
    
    
    
}



















